const app = getApp()
const i18n = require('../../../utils/i18n/index')

Page({
  data: {
    id: '',
    detail: {},
    isInterested: false,
    cdnUrl: '',
    locale: 'zh_CN',
    t: {}
  },

  onLoad(options) {
    this.setData({
      id: options.id,
      cdnUrl: app.globalData.cdnUrl
    })
    this.setI18nData()
    this.loadDetail()
  },

  onShow() {
    wx.setNavigationBarTitle({
        title: i18n.getLocale()=='en_US'?'Member details':'成员详情'
    });
    getApp().loadUserInfo()
  },

  setI18nData() {
    this.setData({
      locale: i18n.getLocale(),
      t: {
        contact: i18n.t('supply.detail.contact'),
        contactName: i18n.t('supply.detail.contactName'),
        contactPhone: i18n.t('supply.detail.contactPhone'),
        description: i18n.t('supply.detail.mdescription'),
        categories: i18n.t('supply.detail.mcategories'),
        mainbuss: i18n.t('supply.detail.mainbuss'),
        interested: i18n.t('supply.detail.interested'),
        interest: i18n.t('supply.detail.interest'),
        share: i18n.t('supply.detail.share'),
        formTitle: i18n.t('supply.detail.formTitle'),
        warmming: i18n.t('supply.detail.warmming'),
        form: {
          name: i18n.t('supply.detail.form.name'),
          namePlaceholder: i18n.t('supply.detail.form.namePlaceholder'),
          company: i18n.t('supply.detail.form.company'),
          companyPlaceholder: i18n.t('supply.detail.form.companyPlaceholder'),
          position: i18n.t('supply.detail.form.position'),
          positionPlaceholder: i18n.t('supply.detail.form.positionPlaceholder'),
          contact: i18n.t('supply.detail.form.contact'),
          contactPlaceholder: i18n.t('supply.detail.form.contactPlaceholder'),
          message: i18n.t('supply.detail.form.message'),
          messagePlaceholder: i18n.t('supply.detail.form.messagePlaceholder'),
          submit: i18n.t('supply.detail.form.submit')
        }
      }
    })
  },

  onLanguageChange() {
    this.setI18nData()
  },

  // 加载详情
  loadDetail() {
    wx.request({
      url: app.globalData.baseUrl + 'public/member/' + this.data.id,
      data: { 
      },
      header: {
        'Authorization': `Bearer ${app.globalData.token}`
      },
      success: res => {
        if (res.data.code === 0) {
          this.setData({
            detail: res.data.data.needs,
            isInterested: res.data.data.is_interested
          })
        }
      }
    })
  },


  // 预览图片
  previewImage(e) {
    const { url } = e.currentTarget.dataset
    const { images } = this.data.detail
    const urls = images.map(img => this.data.cdnUrl + img)
    wx.previewImage({
      current: this.data.cdnUrl + url,
      urls
    })
  },

  // 显示感兴趣表单
  showInterestForm() {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录后再操作',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/mine/login/login'
            })
          }
        }
      })
      return
    }

    if (app.globalData.userInfo.company == "") {
        wx.showModal({
            title: '提示',
            content: '请补充完整个人信息',
            success: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/mine/profile/profile'
                })
              }
            }
        })
        return
    }
    this.setData({ showForm: true })
  },

  // 隐藏感兴趣表单
  hideInterestForm() {
    this.setData({ showForm: false })
  },

  // 提交感兴趣表单
  submitInterest(e) {
    const formData = e.detail.value
    
    // 表单验证
    if (!formData.name || !formData.company || !formData.position) {
      wx.showToast({
        title: i18n.t('supply.detail.form.incomplete'),
        icon: 'none'
      })
      return
    }

    if (!formData.contact) {
      wx.showToast({
        title: i18n.t('supply.detail.form.contactRequired'),
        icon: 'none'
      })
      return
    }

    wx.showLoading({ title: i18n.t('common.loading') })

    wx.request({
      url: `${app.globalData.baseUrl}api/join`,
      method: 'POST',
      header: {
        'Authorization': `Bearer ${app.globalData.token}`
      },
      data: {
        need_id: this.data.detail.id,
        type:1,
        ...formData
      },
      success: res => {
        if (res.data.code === 0) {
          wx.showModal({
            title: '提示',  // 模态框标题
            content: i18n.t('supply.detail.form.submitSuccess'),  // 你要显示的长内容
            showCancel: false,  // 是否显示取消按钮
            confirmText: '确定',  // 确认按钮的文字
            success: function(res) {
              if (res.confirm) {
                // 用户点击了确定按钮的回调
              }
            }
          });
         
          this.setData({
            isInterested: !this.data.isInterested
          })
          this.hideInterestForm()
        } else {
            wx.showModal({
                title: '提示',  // 模态框标题
                content: res.data.msg,  // 你要显示的长内容
                showCancel: false,  // 是否显示取消按钮
                confirmText: '确定',  // 确认按钮的文字
                success: function(res) {
                  if (res.confirm) {
                    // 用户点击了确定按钮的回调
                  }
                }
              });

          if (res.data.code == 110) {
            setTimeout(() => {
                //wx.navigateBack()
                wx.navigateTo({
                    url: '/pages/mine/profile/profile'
                })
              }, 2000)
          }
        }
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 分享按钮点击
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  // 分享
  onShareAppMessage() {
    return {
      title: this.data.detail.name,
      path: `/pages/member/detail/detail?id=${this.data.detail.id}`
    }
  },
  onShareTimeline() {
    return {
      title: this.data.detail.name,
      path: `/pages/member/detail/detail?id=${this.data.detail.id}`
    }
  }
}) 